<template>
  <div class="app-container">
    <el-form v-show="showSearch" ref="queryForm" :inline="true" :model="queryParams" label-width="68px">
      <el-form-item label="小区" prop="areaName">
        <el-select v-model="queryParams.areaName" :filter-method="filterMethod" allow-create clearable
                   default-first-option
                   filterable placeholder="请输入小区" size="small" @change="getGroup(queryParams.areaName)">
          <el-option v-for="(item, index) in areaIdOptions" :key="index" :disabled="item.disabled"
                     :label="item.areaName" :value="item.areaName"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="楼宇" prop="groupName">
        <el-select size="small" v-model="queryParams.groupName" :style="{width: '100%'}" clearable placeholder="请输入楼宇"
                   @change="getUnit(queryParams.areaName,queryParams.groupName)">
          <el-option v-for="(item, index) in groupIdOptions" :key="index" :disabled="item.disabled"
                     :label="item.groupName" :value="item.groupName"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="单元" prop="unitName">
        <el-select size="small" v-model="queryParams.unitName" :style="{width: '100%'}" clearable placeholder="请输入单元">
          <el-option v-for="(item, index) in unitNameOptions" :key="index" :disabled="item.disabled"
                     :label="item.unitName" :value="item.unitName"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="户" prop="doorName">
        <el-input
          v-model="queryParams.doorName"
          :style="{width: '100%'}"
          size="small" clearable
          placeholder="请输入户名称"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="房屋编号" prop="houseId">
        <el-input
          v-model="queryParams.houseId"
          :style="{width: '100%'}"
          size="small" clearable
          placeholder="请输入房屋编号"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="姓名" prop="userName">
        <el-input
          v-model="queryParams.userName"
          :style="{width: '100%'}"
          size="small" clearable
          placeholder="请输入姓名"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="建户时间">
        <el-date-picker
          v-model="dateRange"
          end-placeholder="结束日期"
          range-separator="-"
          size="small"
          start-placeholder="开始日期"
          style="width: 240px"
          type="daterange"
          value-format="yyyy-MM-dd"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button icon="el-icon-search" type="primary"  size="mini"  @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh"  size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">

      <el-col :span="1.5">
        <el-button
          :disabled="single"
          icon="el-icon-edit"
          plain
          size="mini"
          type="success"
          @click="handleUpdate"
        >缴费
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="doorList" @selection-change="handleSelectionChange">
      <el-table-column align="center" type="selection" width="55"/>
      <el-table-column align="center" label="地址" prop="dooraddr" width="200"/>
      <el-table-column align="center" label="房屋编号" prop="houseId"/>
      <el-table-column  :formatter="numberFormat"  align="center" label="面积" prop="space"/>
      <el-table-column align="center" label="实测状态" prop="spaceSts">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.HOUSE005" :value="scope.row.spaceSts"/>
        </template>
      </el-table-column>
      <el-table-column align="center" label="房屋用途" prop="purposeName"/>
      <el-table-column align="center" label="姓名" prop="userName" width="120"/>
      <el-table-column align="center" label="电话" prop="tel" width="120"/>
      <el-table-column align="center" label="证件号码" prop="papersCode" width="120" show-overflow-tooltip/>
      <el-table-column  :formatter="numberFormat"  align="center" label="应缴金额" prop="budgetMoney"/>
      <el-table-column  :formatter="numberFormat"  align="center" label="已缴金额" prop="costMoney"/>
      <el-table-column align="center" label="最后缴费时间" prop="costDate" width="120">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.costDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="缴费状态" prop="costSts">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.HOUSE003" :value="scope.row.costSts"/>
        </template>
      </el-table-column>
      <el-table-column  :formatter="numberFormat"  align="center" label="本金余额" prop="balance"/>
      <el-table-column align="center" label="计息方式" prop="rateType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.HOUSE004" :value="scope.row.rateType"/>
        </template>
      </el-table-column>
      <el-table-column  :formatter="numberFormat"  align="center" label="利息合计" prop="rateMoney"/>
      <el-table-column align="center" label="计息时间" prop="rateDate" width="120">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.rateDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="创建者" prop="creater"/>
      <el-table-column align="center" label="创建时间" prop="createDate" width="120">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="备注" prop="remark"/>
      <el-table-column align="center" class-name="small-padding fixed-width" fixed="right" label="操作">
        <template slot-scope="scope">
          <el-button
            :style="{ display: scope.row.areaName=='合计'?'none':'' }"
            icon="el-icon-edit"
            size="mini"
            type="text"
            @click="handleUpdate(scope.row)"
          >缴费
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :limit.sync="queryParams.pageSize"
      :page.sync="queryParams.pageNum"
      :total="total"
      @pagination="getList"
    />

    <div ref="print" class="print-wrap" hidden>
      <div :style="{ width: '320px' }" class="print-preview">
        <div class="title">打印票据</div>
        <div class="divide_line"></div>
        <!-- ...小票内容html -->
        <div class="tip">请保存好小票，不要丢弃！</div>
        <!-- 底下logo二进制文件流，一定要用二进制文件流，保证浏览器打印能够拿到log -->
        <!-- <img class="image-wrap" src="data:image/png;base64,二进制文件流">-->
        <div id="year" class="div1">ddd</div>
        <div id="month" class="div2"></div>
        <div id="date" class="div3"></div>
        <div id="userName" class="div5"></div>
        <div id="space" class="div6"></div>
        <div id="addr" class="div7"></div>
        <div id="tel" class="div8"></div>
        <div id="formula" class="div10"></div>
        <div id="moneyTemp" class="div11"></div>
        <div id="money" class="div12"></div>
        <div id="unit" class="div13"></div>
      </div>
    </div>

    <!-- 添加或修改户管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" append-to-body width="800px">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="小区" prop="areaName">
              <el-select v-model="form.areaName" :disabled="true" :style="{width: '100%'}" clearable
                         placeholder="请输入小区" @change="getGroup(form.areaName)">
                <el-option v-for="(item, index) in areaIdOptions" :key="index" :disabled="false"
                           :label="item.areaName" :value="item.areaName"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="楼宇" prop="groupName">
              <el-select v-model="form.groupName" :disabled="true" :style="{width: '100%'}" clearable
                         placeholder="请输入楼宇" @change="getUnit(form.areaName,form.groupName)">
                <el-option v-for="(item, index) in groupIdOptions" :key="index" :disabled="item.disabled"
                           :label="item.groupName" :value="item.groupName"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="单元" prop="unitName">
              <el-select v-model="form.unitName" :disabled="true" :style="{width: '100%'}" clearable
                         placeholder="请输入单元">
                <el-option v-for="(item, index) in unitNameOptions" :key="index" :disabled="item.disabled"
                           :label="item.unitName" :value="item.unitName"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="户" prop="doorName">
              <el-input v-model="form.doorName" :disabled="true" placeholder="请输入户名称"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="面积" prop="space">
              <el-input v-model="form.space" :disabled="true" placeholder="请输入面积"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="资金余额" prop="balance">
              <el-input v-model="form.balance" :disabled="true" placeholder=""/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="利息合计" prop="rateMoney">
              <el-input v-model="form.rateMoney" :disabled="true" placeholder=""/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="应缴金额" prop="budgetMoney">
              <el-input v-model="form.budgetMoney" :disabled="true" placeholder=""/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="缴费金额" prop="costMoney">
              <el-input v-model="form.costMoney" :disabled="true" placeholder=""/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="姓名" prop="userName">
              <el-input v-model="form.userName" placeholder="请输入姓名"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="电话" prop="tel">
              <el-input v-model="form.tel" placeholder="请输入电话"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="证件号码" prop="papersCode">
              <el-input v-model="form.papersCode" placeholder="请输入证件号码"/>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="交款类型">
              <el-select v-model="form.costBillType" :style="{width: '100%'}" placeholder="请选择">
                <el-option
                  v-for="dict in dict.type.HOUSE022"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="收款方式">
              <el-select v-model="form.costBillWay" :style="{width: '100%'}" placeholder="请选择">
                <el-option
                  v-for="dict in dict.type.HOUSE021"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="缴费时间" prop="costDate">
              <el-date-picker
                v-model="form.costDate"
                :style="{width: '100%'}"
                placeholder="选择缴费时间"
                type="date" value-format="yyyy-MM-dd">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="备注" prop="remark">
              <el-input v-model="form.remark" placeholder="请输入内容" type="textarea"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button type="warning" @click="weChatPay">微信扫码支付</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {getDoor,insertCost,listDoor,weChatPay} from "@/api/fund/cost";
import {getAreaInfo,getAreaInfo1} from "@/api/house/area";
import {getGroupInfo} from "@/api/house/group";
import {getUnitInfo} from "@/api/house/unit";
import {getPurposeInfo} from "@/api/house/purpose";

export default {
  name: "Cost",
  dicts: ['HOUSE003', 'HOUSE004', 'HOUSE005', 'HOUSE022', 'HOUSE021'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 日期范围
      dateRange: [],
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 户管理表格数据
      doorList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        areaName: null,
        groupName: null,
        unitName: null,
        doorName: null,
        houseId: null,
        purposeId: null,
        userName: null,
      },
      areaIdOptions: [{}],
      groupIdOptions: [{}],
      unitNameOptions: [{}],
      purposeOptions: [{}],
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        costDate: [
          { required: true, message: "缴费时间不能为空", trigger: "change" }
        ],
      /*  userName: [
          { required: true, message: "姓名不能为空", trigger: "change" }
        ],
        tel: [
          { required: true, message: "电话不能为空", trigger: "change" }
        ],
        papersCode: [
          { required: true, message: "证件号码不能为空", trigger: "change" }
        ],*/
        costBillType: [
          { required: true, message: "不能为空", trigger: "change" }
        ],
        costBillWay: [
          { required: true, message: "不能为空", trigger: "change" }
        ],
      }
    };
  },
  created() {
    this.getList();
    this.areaSelect();
    this.filterMethod();
    this.purposeSelect();
  },
  methods: {
    numberFormat (row, column, cellValue) {
      cellValue += ''
      if (!cellValue.includes('.')) cellValue += '.'
      return cellValue.replace(/(\d)(?=(\d{3})+\.)/g, function ($0, $1) {
        return $1 + ','
      }).replace(/\.$/, '')
    },
    filterMethod(val) {
      this.value = val
      getAreaInfo1(this.value).then(response => {
        this.areaIdOptions = response.rows;
      });
    },
    areaSelect() {
      getAreaInfo().then(response => {
        this.areaIdOptions = response.rows;
      });
    },
    getGroup(areaName) {
      if (areaName != "") {
        getGroupInfo(areaName).then(response => {
          this.groupIdOptions = response.rows;
        });
      }
    },
    getUnit(areaName, groupName) {
      getUnitInfo(areaName, groupName).then(response => {
        this.unitNameOptions = response.rows;
      });
    },
    purposeSelect() {
      getPurposeInfo().then(response => {
        this.purposeOptions = response.rows;
      });
    },
    /** 查询户管理列表 */
    getList() {
      this.loading = true;
        listDoor(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
        this.doorList = response.rows;
        this.total = response.total;
        this.loading = false;
        this.areaSelect();
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        doorId: null,
        areaName: null,
        groupName: null,
        unitName: null,
        doorName: null,
        houseId: null,
        space: null,
        spaceSts: null,
        purposeId: null,
        userName: null,
        tel: null,
        papersCode: null,
        budgetMoney: null,
        costMoney: null,
        costDate: null,
        costSts: null,
        balance: null,
        rateType: null,
        rateMoney: null,
        rateDate: null,
        creater: null,
        createDate: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.areaSelect();
      this.resetForm("queryForm");
      this.dateRange = [];
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.doorId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },

    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const doorId = row.doorId || this.ids
      getDoor(doorId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "缴费";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          insertCost(this.form).then(response => {
            this.$modal.msgSuccess("缴费成功");
            this.open = false;
            this.getList();
          });
        }
      });
    },
    //扫码支付
    weChatPay() {
      debugger
      this.$refs["form"].validate(valid => {
        if (valid) {
          return weChatPay(this.form).then(response => {
            let baseUrl = "http://" + response.data.addr;
            window.open(baseUrl);
          });
        }
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('fund/cost/export', {
        ...this.queryParams
      }, `door_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
